/**
 * 
 */
package com.game.astd.spirit;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.gargoylesoftware.htmlunit.util.NameValuePair;
import com.game.astd.spirit.inter.AbstractXmlActionAfterRequest;
import com.game.astd.spirit.inter.TaskUnit;
import com.game.astd.spirit.model.UserData;
import com.game.astd.spirit.model.XmlDataUtil;
import com.game.astd.spirit.model.XmlNodeData;

/**
 * 玉石任务
 * 
 * @author beedoorwei
 * 
 */
public class BowlderResTask extends TaskUnit
{
	public BowlderResTask()
	{
		this.username = "玉石矿任务";
	}

	@Override
	public void doTask()
	{
		try
		{
			Iterator<String> ite = GameUserInfoUtil.userInfoCache.keySet().iterator();
			while (ite.hasNext())
			{
				String username = ite.next();
				TaskManager.addTask(new BowlderRes(username), GameUtil.randomInt(10));
			}
		} catch (Exception e)
		{
			LoggerUtil.error(e);
		}
	}

	/**
	 * 玉石任务
	 * 
	 * @author Administrator
	 * 
	 */
	class BowlderRes extends TaskUnit
	{
		int scopeId = 0;

		public BowlderRes(String username)
		{
			this.username = username;
		}

		public BowlderRes(String username, int scopeId)
		{
			this.username = username;
			this.scopeId = scopeId;
		}

		public void doTask()
		{
			try
			{
				UserData u = GameUserInfoUtil.getUser(username);
				if (u.getUserState() == UserState.LOGIN_GAME)
				{
					List<NameValuePair> nvpList = new ArrayList<NameValuePair>();
					nvpList.add(new NameValuePair("scopeId", scopeId + ""));
					HttpUtil.doPost(username, GameConstants.BOWLDER_RES_INFO_URL, nvpList, new BowlderResInfo(username, scopeId));
				} else
				{
					// 添加登录任务先
					TaskManager.addTask(new LoginTask(username, new BowlderRes(username)));
				}
			} catch (Exception e)
			{
				LoggerUtil.error(e);
			}
		}
	}

	/**
	 * 玉石矿前查询
	 * 
	 * @author Administrator
	 * 
	 */
	class BowlderResInfo extends AbstractXmlActionAfterRequest
	{
		int scopeId = 0;

		public BowlderResInfo(String username, int scopeId)
		{
			super();
			this.username = username;
			this.scopeId = scopeId;
			this.taskUnit = new BowlderRes(username);
		}

		@Override
		public void doCall(List<NameValuePair> headerList, XmlNodeData bowlderResInfo)
		{
			String s = XmlDataUtil.getNodeData(bowlderResInfo, "results.hasres");
			if (s.equals("1"))
			{
				// 获取cd时间
				Map<String, Integer> m = getResCdTime(bowlderResInfo);
				if (m.get("resouceid") != -1)
				{
					// 再判断下是否可以抢收
					if (m.get("canrush") == 1)
					{
						LoggerUtil.info("当前已经占矿，所占矿区:" + (scopeId+1) + " 可以抢收");
						// 先抢收
						List<NameValuePair> nvpList = new ArrayList<NameValuePair>();
						nvpList.add(new NameValuePair("scopeId", String.valueOf(scopeId)));
						nvpList.add(new NameValuePair("resId", String.valueOf(m.get("resouceid"))));
						HttpUtil.doPost(username, GameConstants.BOWLDER_RES_RUSH_URL, nvpList, new BowlderRushAction(username, scopeId, String.valueOf(m.get("resouceid"))));
					} else
					{
						LoggerUtil.info("当前已经占矿，所占矿区:" + (scopeId+1) + " 无法抢收，剩余时间：" + m.get("remain") / 60 / 60 + "小时" + m.get("remain") / 60 % 60 + "分钟" + m.get("remain") % 60 + "秒");
						// 等下次占矿咯
						TaskManager.addTask(new BowlderRes(username), m.get("remain"));
					}
				} else
				{
					LoggerUtil.info("第[" + (scopeId+1) + "]矿区，用户[" + GameUserInfoUtil.getGameName(username) + "]没有占矿 ");
					List<NameValuePair> nvpList = new ArrayList<NameValuePair>();
					nvpList.add(new NameValuePair("scopeId", String.valueOf(scopeId + 1)));
					HttpUtil.doPost(username, GameConstants.BOWLDER_RES_INFO_URL, nvpList, new BowlderResInfo(username, scopeId + 1));
				}
			} else
			{
				// 没占玉石矿，找空闲的搞呗
				List<XmlNodeData> bowlderResListInfo = XmlDataUtil.getNodeInfoList(bowlderResInfo, "results.res");
				boolean isFind = false;
				for (XmlNodeData x : bowlderResListInfo)
				{
					if (XmlDataUtil.getNodeData(x, "playerid").equals("0"))
					{
						isFind = true;
						LoggerUtil.info("第:" + (scopeId+1) + " 矿区第" + XmlDataUtil.getNodeData(x, "resouceid") + "号矿无人占领，搞呗：" + GameUserInfoUtil.getGameName(username));
						// 没有人占矿,搞了呗
						List<NameValuePair> nvpList = new ArrayList<NameValuePair>();
						nvpList.add(new NameValuePair("scopeId", scopeId + ""));
						nvpList.add(new NameValuePair("resId", XmlDataUtil.getNodeData(x, "resouceid")));
						HttpUtil.doPost(username, GameConstants.BOWLDER_RES_ATTACK_URL, nvpList, new BowlderAttackAction(username, scopeId));
						break;
					}
				}
				if (!isFind)
				{
					// 没有找到,找下一矿区
					LoggerUtil.info("第[" + (scopeId+1) + "]矿区，用户[" + GameUserInfoUtil.getGameName(username) + "]在此没有找到空闲的矿 ");
					List<NameValuePair> nvpList = new ArrayList<NameValuePair>();
					nvpList.add(new NameValuePair("scopeId", String.valueOf(scopeId + 1)));
					HttpUtil.doPost(username, GameConstants.BOWLDER_RES_INFO_URL, nvpList, new BowlderResInfo(username, scopeId + 1));
				}
			}
		}

		/**
		 * 查找该矿区是否有当前用户的矿，如果有返回该矿的cd时间，如果没有，这返回-1
		 * 
		 * @param bowlderResInfo
		 * @return 玉石矿的cd时间，以秒为计时单位
		 */
		private Map<String, Integer> getResCdTime(XmlNodeData bowlderResInfo)
		{
			Map<String, Integer> m = new HashMap<String, Integer>();
			int remain = -1;
			int resouceid = -1;
			int canrush = -1;
			List<XmlNodeData> bowlderResListInfo = XmlDataUtil.getNodeInfoList(bowlderResInfo, "results.res.remaintime");
			XmlNodeData userRes = null;

			for (XmlNodeData xl : bowlderResListInfo)
			{
				if (!xl.getNodeValue().equals("0"))
				{
					// 找到了所占领的资源id
					userRes = xl.getParent();
					break;
				}
			}
			if (userRes != null)
			{
				remain = GameUtil.convertInt(XmlDataUtil.getNodeData(userRes, "remaintime")) / 1000 + 1;
				resouceid = GameUtil.convertInt(XmlDataUtil.getNodeData(userRes, "resouceid"));
				canrush = GameUtil.convertInt(XmlDataUtil.getNodeData(userRes, "canrush"));
			}

			m.put("remain", remain);
			m.put("resouceid", resouceid);
			m.put("canrush", canrush);
			return m;
		}
	}

	class BowlderRushAction extends AbstractXmlActionAfterRequest
	{
		int scopeId = 0;
		String resid;

		public BowlderRushAction(String username, int scopeId, String resId)
		{
			super();
			this.username = username;
			this.scopeId = scopeId;
			this.resid = resId;
			this.taskUnit = new BowlderRes(username);
		}

		@Override
		public void doCall(List<NameValuePair> headerList, XmlNodeData bowlderResInfo)
		{
			LoggerUtil.info("抢收成功:" + (scopeId+1) + "矿区， 资源id:" + resid + " 4小时后继续");
			TaskManager.addTask(new BowlderRes(username), 4 * 60 * 60);
		}

	}

	class BowlderAttackAction extends AbstractXmlActionAfterRequest
	{
		int scopeId = 0;

		public BowlderAttackAction(String username, int scopeId)
		{
			super();
			this.username = username;
			this.scopeId = scopeId;
			this.taskUnit = new BowlderRes(username);
		}

		@Override
		public void doCall(List<NameValuePair> headerList, XmlNodeData bowlderResInfo)
		{

			String bowlderResListInfo = XmlDataUtil.getNodeData(bowlderResInfo, "results.battlereport.winside");
			if (bowlderResListInfo.equals("1"))
			{
				LoggerUtil.info("占矿成功" + GameUserInfoUtil.getGameName(username));
			} else
			{
				LoggerUtil.info("占矿失败" + GameUserInfoUtil.getGameName(username));
			}
			TaskManager.addTask(new BowlderRes(username, scopeId));
		}
	}
}